
# REQUIRES: Data/Roper Data.csv

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Roper/Cleaning.R")

################# Figure 1 - US Public Support for Withdrawal Over Time #################

### Scatter Plot
roper$startyear <-as.Date(roper$Startdate,'%Y-%m-%d')
roper$startyear <- format(as.Date(roper$startyear, format="%Y/%m/%d"),"%Y")
roper$startyear <- as.numeric(roper$startyear)

roper$endyear <-as.Date(roper$Enddate,'%Y-%m-%d')
roper$endyear <- format(as.Date(roper$endyear, format="%Y/%m/%d"),"%Y")
roper$endyear <- as.numeric(roper$endyear)

roper$surveyyear <- (roper$startyear+roper$endyear)/2

### Figure 1
margin <- unit(0.5, "line")
grob1 <- ggplot(roper, aes(x=surveyyear, y=percwithdrawd)) +
  geom_point(aes(col = Campaign2, shape = Campaign2)) +
  geom_smooth(method="loess", alpha=0.16, size=0.7, color="#d3d3d3", se = FALSE) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits=c(0,1)) +
  scale_x_continuous(limits=c(1940,2022)) +
  labs(x = "Date of Poll", y = "Support for Withdrawal", col='Campaign', shape="Campaign") +
  theme_minimal() +
  theme(text=element_text(family="Times New Roman"))
#ggtitle("Figure 1: U.S. Public Support for Withdrawal Over Time","Compilation of Polls from 1946-2021") +
#theme(plot.title = element_text(hjust = 0.5, face = "bold"), plot.subtitle = element_text(hjust = 0.5), plot.background = element_rect(colour = "black", fill = "white", size=0))
grob2 <- textGrob("Figure 1: U.S. Public Support for Withdrawal Over Time", gp=gpar(fontsize=13,fontface = "bold", fontfamily="Times New Roman"))
grob3 <- textGrob("Compilation of Polls from 1946-2021", gp=gpar(fontsize=11, fontfamily="Times New Roman"))

plot1 <- grid.arrange(grob1, grob2, grob3, 
                      heights = unit.c(unit(1,"null"), grobHeight(grob2) + 1.2*margin, grobHeight(grob3) + margin),
                      vp=viewport(width=0.95, height=0.95))

ggsave("Plots/Figure 1 - US Public Support for Withdrawal Over Time.png", plot1)


grob1 <- ggplot(roper, aes(x=surveyyear, y=percwithdrawd)) +
  geom_point(aes(col = Campaign2, shape = Campaign2)) +
  geom_smooth(method="loess", alpha=0.16, size=0.7, color="#d3d3d3", se = FALSE) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits=c(0,1)) +
  scale_x_continuous(limits=c(1940,2022)) +
  labs(x = "Date of Poll", y = "Support for Withdrawal", col='Campaign', shape="Campaign") +
  theme_minimal() +
  theme(text=element_text(family="Times New Roman", size = 18), panel.background = element_rect(fill = 'white', colour = 'white'),
        plot.background = element_rect(fill = 'white', colour = 'white'))
ggsave("Plots/Silverman POQ-23-0350.R1 Figure 1.eps", grob1)

rm(grob1, grob2, grob3, plot1, margin)
